/**
 * 
 */
package d.qapaper.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.Log;

/**
 * @author Kaven
 * 2011-3-23
 */
public class ImageUtil {
    private static final String LOG_TAG = "[ImageUtil]";
    
    public static Drawable loadImageFromUrl(String url) {   
        URL imgURL;   
        InputStream is = null; 
        HttpURLConnection connection = null;
        Drawable d = null;
        try {   
            imgURL = new URL(url);   
            connection =(HttpURLConnection) imgURL.openConnection();
            connection.setDoInput(true);
            connection.connect();
            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                is = connection.getInputStream();
                d = Drawable.createFromStream(is, "src");
                is.close();
            }
        } catch (Exception ex) {   
            Log.e(LOG_TAG, ex.getMessage()); 
        } finally {
            if(connection != null) {
                connection.disconnect();
            }
        }
        return d;   
    }
    
	/**
	 * 用於網絡獲取圖片
	 * @param url
	 * @return
	 */
	public static Bitmap  returnBitmap(String url){
		URL myFileUrl = null;
		Bitmap bitmap=null;
		HttpURLConnection connection = null;
		try {
			myFileUrl=new URL(url);
			connection =(HttpURLConnection)myFileUrl.openConnection();
			//connection.setDoInput(true);
			connection.connect();
			if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
			    InputStream is = connection.getInputStream();
	            bitmap = BitmapFactory.decodeStream(is);
	            is.close();
			}
		} catch (Exception e) {
			Log.e(LOG_TAG, e.getMessage());
		} finally {
		    if(connection != null) {
		        connection.disconnect();
		    }
		}
		return bitmap;
	}
	
	private static Bitmap getNativeBitmap(String path){
		Bitmap bitmap=null;
		try {
			File file=new File(path);
			
			if(file.exists()){
				InputStream inputStream=new FileInputStream(file);
				bitmap = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
			}
		} catch (Exception e) {
			Log.e(LOG_TAG, e.getMessage());
		}
		
		return bitmap;
	}
	
	/**
	 * 獲取圓角圖片
	 * @param bitmap
	 * @param roundPx
	 * @param type 1：網絡圖片 2：本地圖片
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(String url,float roundPx,int type){   
		Bitmap inputBitmap=null;
		switch (type) {
		case 1:
			inputBitmap=ImageUtil.returnBitmap(url);
			break;
		case 2:
			inputBitmap=ImageUtil.getNativeBitmap(url);
			break;
		}
        Bitmap output = Bitmap.createBitmap(inputBitmap.getWidth(), inputBitmap   
                .getHeight(), Config.ARGB_8888);   
        Canvas canvas = new Canvas(output);   
        final int color = 0xff424242;   
        final Paint paint = new Paint();   
        final Rect rect = new Rect(0, 0, inputBitmap.getWidth(), inputBitmap.getHeight());   
        final RectF rectF = new RectF(rect);   
    
        paint.setAntiAlias(true);   
        canvas.drawARGB(0, 0, 0, 0);   
        paint.setColor(color);   
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   
    
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   
        canvas.drawBitmap(inputBitmap, rect, rect, paint);   
    
        return output;   
    }   

	
	private static Bitmap fitSizePic( byte[] f) {  
        Bitmap resizeBmp = null;  
        BitmapFactory.Options opts = new BitmapFactory.Options();  
     // 数字越大读出的图片占用的heap越小 不然总是溢出  
        if (f.length < 2048) { // 0-20k  
            opts.inSampleSize = 1;  
        } else if (f.length < 5120) { // 20-50k  
            opts.inSampleSize = 2;  
        } else if (f.length < 30720) { // 50-300k  
            opts.inSampleSize = 4;  
        } else if (f.length < 81920) { // 300-800k  
            opts.inSampleSize = 6;  
        } else if (f.length < 104857) { // 800-1024k  
            opts.inSampleSize = 8;  
        } else {  
            opts.inSampleSize = 60;  
        }  
        resizeBmp = BitmapFactory.decodeByteArray(f, 0, f.length, opts);  
        return resizeBmp;  
    }  
}
